﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Controllers
{
    public class SiteController : Controller
    {
        private readonly Manager.BLL.Admin.M_SiteBll bll = new Manager.BLL.Admin.M_SiteBll();

        //
        // GET: /Admin/Site/
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.SiteSet, Ope = "view")]
        public ActionResult Index()
        {
            Manager.Model.Admin.M_Site model = new Manager.Model.Admin.M_Site();
            if (bll.GetModel(1) != null)
            {
                model = bll.GetModel(1);
                return View(model);
            }
            else
            {
                return View();
            }
            
        }

        [HttpPost]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.SiteSet, Ope = "view")]
        public ActionResult Index(Manager.Model.Admin.M_Site model)
        {
            if (ModelState.IsValid)
            {
                if (bll.GetRecordCount("") == 0)
                {
                    int r = bll.Add(model);
                    if (r > 0)
                        return RedirectToAction("Index", "Site");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
                else
                {
                    model.SiteID = 1;
                    bool r = bll.Update(model);
                    if(r)
                        return RedirectToAction("Index", "Site");
                    ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            return View(model);
        }
    }
}
